from django.shortcuts import render
from rest_framework.response import Response
from .models import UserModel
from rest_framework.views import APIView
import re,random,redis
# Create your views here.
class GetNumber(APIView):
    def get(self,request,name):
        num = random.randint(100,300)
        n = redis.Redis(host='127.0.0.1',port=6379,db=5)
        n.set(name,num,ex=120)
        n.close()
        return Response({'msg':'ok','code':200,'num':num})

class Register(APIView):
    def get(self,request,mobile):
        if not re.findall(r'^1[3-9]\d{9}$',mobile):
            return Response({'msg':'手机号码格式不对','code':400})
        code = random.randint(10000,99999)
        r = redis.Redis(host='127.0.0.1',port=6379,db=5)
        r.set(mobile,code,ex=120)
        r.close()
        return Response({'msg':'ok','code':200,'sms_code':code})

    def post(self,request,mobile):
        code1 = request.data.get('code')
        name = request.data.get('name')
        password = request.data.get('password')
        mobile = request.data.get('mobile')
        money = request.data.get('money')
        if not all([name,password,mobile,money]):
            return Response({'msg':'前端数据不完整'},status=400)
        user = UserModel.objects.filter(name=name).first()
        if user:
            return Response({'msg':'该用户已经存在'},status=400)
        if not re.findall(r'^1[3-9]\d{9}$',mobile):
            return Response({'msg':'手机号码格式不对','code':400})
        r = redis.Redis(host='127.0.0.1',port=6379,db=5)
        code2 = r.get(mobile)
        r.close()
        if code2:
            code2 = code2.decode()
        else:
            return Response({'msg':'验证码已经过期','code':401})
        if str(code1) != str(code2):
            return Response({'msg':'验证码不正确','code':402})
        UserModel.objects.create(name=name,password=password,mobile=mobile,money=money)
        return Response({'msg':'用户创建完成','code':200})

class Login(APIView):
    def post(self,request):
        name = request.data.get('name')
        password = request.data.get('password')
        if not all([name,password]):
            return Response({'msg':'前端数据不完整'},status=400)
        user = UserModel.objects.filter(name=name).first()
        if not user:
            return Response({'msg':'没有找到这个用户'},status=400)
        UserModel.objects.get(name=name,password=password)
        return Response({'msg':'ok'},status=200)

class CheckOneUser(APIView):
    def get(self,request,name):
        user = UserModel.objects.get(name=name)
        if not user:
            return Response({'msg':'没有找到这个用户'})
        user_list = [{'id':user.id,'money':user.money}]
        print("该用户的金额是>>>>",user_list)
        return Response({'msg':'ok','data':user_list},status=200)

class AddNum(APIView):
    def post(self,request,name):
        user = UserModel.objects.get(name=name)
        money = request.data.get('num')
        if not user:
            return Response({'msg':'没有找到这个用户'},status=400)
        num = int(user.money) + int(money)
        user.money = str(num)
        user.save()
        # print("充值后的金额为>>>",num)
        return Response({'msg':'ok','data':user.money},status=200)

class BalanceView(APIView):
    def put(self,request,name):
        user = UserModel.objects.get(name=name)
        r = redis.Redis(host='127.0.0.1',port=6379,db=5)
        code2 = r.get(name)
        if code2:
            num = code2.decode()
            print(">>>>>>>>",num)
            money = int(num) * 0.5
            user.balance = int(user.balance) - int(money)
            user.save()
            if user.balance >= 0:
                return Response({'msg':'结算成功','data':user.balance,'code':200})
            else:
                return Response({'msg':'余额不足，结算失败','code':400})
        else:
            return Response({'msg':'用电量已失效','code':401})

